﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;using System.Configuration;
using System.Windows.Forms;

namespace ChairtyBank.DAL.PreAccept
{
    public class SearchEntity
    {
        public Int32 Search_Id { get; set; }
        public string S_Desision { get; set; }
        public string S_Reason { get; set; }
        public string Search_Akhesaye { get; set; }
    }

    public class Search
    {
        //Set absolute DB path rather than relative to solve openFileDialog reset path problem
        private static string ConnString = ConfigurationManager.ConnectionStrings["BankElKheir"].ToString();
        private static string CmdText = "";

        public static bool Add(SearchEntity s)
        {
            bool flag = true;

            CmdText = "Insert into Search (Search_Id,S_Desision,S_Reason,Search_Akhesaye) values (@Search_Id,@S_Desision,@S_Reason,@Search_Akhesaye)";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);


            cmd.Parameters.AddWithValue("@Search_Id", s.Search_Id);
            cmd.Parameters.AddWithValue("@S_Desision", s.S_Desision);
            cmd.Parameters.AddWithValue("@S_Reason", s.S_Reason);
            cmd.Parameters.AddWithValue("@Search_Akhesaye", s.Search_Akhesaye);

            try
            {
                Con.Open();
                int Num = cmd.ExecuteNonQuery();
                if (Num == 0)
                    flag = false;
            }

            catch (Exception ex)
            {
                MessageBox.Show("خطأ فى ادخال البيانات" + ex.Message);
                flag = false;
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                    flag = false;
                }
            }

            return flag;


        }

        public static bool Update(SearchEntity s)
        {
            bool flag = true;

            CmdText = "Update Search set S_Desision = @S_Desision , S_Reason = @S_Reason , Search_Akhesaye = @Search_Akhesaye where Search_Id = @Search_Id";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@S_Desision", s.S_Desision);
            cmd.Parameters.AddWithValue("@S_Reason", s.S_Reason);
            cmd.Parameters.AddWithValue("@Search_Akhesaye", s.Search_Akhesaye);
            cmd.Parameters.AddWithValue("@Search_Id", s.Search_Id);


            try
            {
                Con.Open();
                int num = cmd.ExecuteNonQuery();
                if (num == 0)
                    flag = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ فى تغيير البيانات " + ex.Message);
                flag = false;
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                    flag = false;
                }
            }

            return flag;

        }

        public static bool Delete(Int32 s)
        {
            bool flag = true;

            CmdText = "Delete from Search where Search_Id = @Search_Id";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@Search_Id", s);

            try
            {
                Con.Open();
                int num = cmd.ExecuteNonQuery();
                if (num == 0)
                    flag = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ اثناء الحذف ", "رساله ادارية " + ex.Message);
                flag = false;
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                    flag = false;
                }
            }

            return flag;
        }

        public static List<SearchEntity> GetSearchByID(Int32 id)
        {
            SearchEntity x;
            List<SearchEntity> SearchList = new List<SearchEntity>();

            CmdText = "Select * from Search where Search_Id = @Search_Id";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@Search_Id", id);

            try
            {
                Con.Open();

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    x = FillEntityFromReader(reader);
                    SearchList.Add(x);
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ عند تحديد البيانات المطلوبة من قاعدة البيانات ", "رساله ادارية " + ex.Message);
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                    Con.Close();
            }

            return SearchList;
        }

        private static SearchEntity FillEntityFromReader(SqlDataReader reader)
        {
            SearchEntity x = new SearchEntity();
            x.Search_Id = Int32.Parse(reader["Search_Id"].ToString());
            if (reader["S_Desision"] != null)
                x.S_Desision = reader["S_Desision"].ToString();
            if (reader["S_Reason"] != null)
                x.S_Reason = reader["S_Reason"].ToString();
            if ((reader["Search_Akhesaye"] != null))
                x.Search_Akhesaye = reader["Search_Akhesaye"].ToString();

            return x;
        }

    }
}
